package com.luban.authserver.config.tokengranter;

import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SmsCodeTokenGranter extends AbstractTokenGranter {
    private static final String SMS_GRANT_TYPE = "sms_code";

    public SmsCodeTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory oAuth2RequestFactory) {
        super(tokenServices,clientDetailsService,oAuth2RequestFactory,SMS_GRANT_TYPE);
    }

    @Override
    protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
        Map<String, String> parameters =
                new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());

        // 客户端提交的手机号码
        String phoneNumber = parameters.get("number");
        if (StringUtils.isBlank(phoneNumber)) {
            throw new RuntimeException("number is null");
        }

        // 客户端提交的验证码
        String smsCode = parameters.get("smscode");
        if (!smsCode.equals("9876")) {
            throw new RuntimeException("smscode is error");
        }

//        // 客户端提交的手机号码
//        AccountDTO accountDTO = accountService.getAccountByPhone(phoneNumber);
//
//        // TODO: if account not exist , create a new account ??
//        if (ObjectUtils.isEmpty(accountDTO)) {
//            throw new BusinessException(ResultCode.PHONE_NOT_EXITS);
//        }

//        // TODO: 权限查询 etc...
//        RoleDTO role = new RoleDTO();
//        role.setRole("USER");
//        //accountService.getAccountRole(accountDTO.getAccountId());

        // 根据手机号码查询用户 ...

        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
//        grantedAuthorities.add(new SimpleGrantedAuthority(role.getRole()));
        UserDetails user = new User("user1","123456",grantedAuthorities);

        // 验证用户状态(是否禁用 etc...)

        Authentication userAuth =
                new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
        // org.springframework.security.core.userdetails.UserDetails 接口的, 所以有 user.getAuthorities()
        // 当然该参数传null也行
//        ((AbstractAuthenticationToken) userAuth).setDetails(parameters);
        OAuth2Request storedOAuth2Request =
                getRequestFactory().createOAuth2Request(client, tokenRequest);
        return new OAuth2Authentication(storedOAuth2Request, userAuth);

    }
}
